package com.mentalhealthplatform.teacherV2.controller;

import com.mentalhealthplatform.appModels.Teacher;
import com.mentalhealthplatform.teacherV2.dto.TeacherDTO;
import com.mentalhealthplatform.teacherV2.service.TeacherServiceV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/v2/teachers")
@CrossOrigin(origins = "http://localhost:3000")
public class TeacherControllerV2 {

    @Autowired
    private TeacherServiceV2 teacherServiceV2;

    /**
     * 获取所有教师信息
     *
     * @return 教师列表的响应实体
     */
    @GetMapping
    public ResponseEntity<List<Teacher>> getAllTeachers() {
        List<Teacher> teachers = teacherServiceV2.findAll();
        return ResponseEntity.ok(teachers);
    }

    /**
     * 获取当前认证的教师信息
     *
     * @param authentication 认证对象
     * @return 当前教师的DTO对象
     */
    @GetMapping("/me")
    public ResponseEntity<TeacherDTO> getCurrentTeacher(Authentication authentication) {
        String username = authentication.getName();
        Teacher teacher = teacherServiceV2.findByUsername(username);
        if (teacher == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(TeacherDTO.convertToDTO(teacher));
    }

    /**
     * 根据ID获取教师信息
     *
     * @param id 教师ID
     * @return 教师的响应实体
     */
    @GetMapping("/{id}")
    public ResponseEntity<Teacher> getTeacherById(@PathVariable UUID id) {
        Teacher teacher = teacherServiceV2.getTeacherById(id);
        if (teacher == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(teacher);
    }

    /**
     * 更新教师信息
     *
     * @param id 教师的UUID
     * @param updatedTeacher 更新后的教师信息
     * @return 更新后的教师DTO对象
     */
    @PutMapping("/{id}")
    public ResponseEntity<TeacherDTO> updateTeacher(@PathVariable UUID id, @RequestBody Teacher updatedTeacher) {
        Teacher existingTeacher = teacherServiceV2.getTeacherById(id);
        if (existingTeacher == null) {
            return ResponseEntity.notFound().build();
        }

        // 更新教师信息
        existingTeacher.setName(updatedTeacher.getName());
        existingTeacher.setNumber(updatedTeacher.getNumber());
        existingTeacher.setGender(updatedTeacher.getGender());
        existingTeacher.setBirthDate(updatedTeacher.getBirthDate());
        existingTeacher.setCollege(updatedTeacher.getCollege());
        existingTeacher.setPhone(updatedTeacher.getPhone());
        existingTeacher.setAddress(updatedTeacher.getAddress());
        existingTeacher.setDepartment(updatedTeacher.getDepartment());

        // 保存更新后的教师信息
        Teacher savedTeacher = teacherServiceV2.saveTeacher(existingTeacher);

        // 返回DTO对象
        return ResponseEntity.ok(TeacherDTO.convertToDTO(savedTeacher));
    }

    /**
     * 更新教师密码
     *
     * @param id 教师的UUID
     * @param passwordMap 包含旧密码和新密码的映射
     * @param authentication 认证对象
     * @return 密码更新状态的响应
     */
    @PutMapping("/{id}/password")
    public ResponseEntity<String> updatePassword(@PathVariable UUID id, @RequestBody Map<String, String> passwordMap, Authentication authentication) {
        String oldPassword = passwordMap.get("oldPassword");
        String newPassword = passwordMap.get("newPassword");

        // 调用服务层方法修改密码
        boolean isPasswordUpdated = teacherServiceV2.updatePassword(id, oldPassword, newPassword);
        if (isPasswordUpdated) {
            return ResponseEntity.ok("密码修改成功");
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("旧密码不正确或密码修改失败");
        }
    }
}